{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🎯 Run CSPOT with Docker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Install Docker on your local machine if you haven't already done so.\n",
    "2. Open a terminal or command prompt on your machine.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download CSPOT from Docker Hub\n",
    "```\n",
    "docker pull nirmallab/cspot:latest\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Run Docker**\n",
    "Running cspot via docker follows the same principles as running cspot via Command Line Interface. \n",
    "  \n",
    "If you are comfortable using Docker and would like to execute the commands in your preferred way, please feel free to do so. However, if you are new to Docker and would like step-by-step instructions, please follow the tutorial below.\n",
    "  \n",
    "**Download the [sample data](https://doi.org/10.7910/DVN/C45JWT)**. Please keep in mind that the sample data is used for demonstration purposes only and has been simplified and reduced in size. It is solely intended for educational purposes on how to execute `cspot` and will not yeild any meaningful results.\n",
    "  \n",
    "**The purpose of this tutorial is solely to demonstrate how to run cspot using Docker. If you require detailed explanations of each step, please refer to the other tutorials.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-1: Generate Thumbnails for Training Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use your own data, it is recommended to follow the same folder structure as the sample data. However, if that is not possible, you should place all the required data within a single folder. This is because we need to tell Docker where to find all the raw data, and specifying a single directory makes it easier to manage the data within the container.\n",
    "  \n",
    "```\n",
    "# specify the directory where the sample data lives and Run the docker command\n",
    "export projectDir=\"/Users/aj/Documents/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                nirmallab/cspot:latest \\\n",
    "                python /app/generateThumbnails.py \\\n",
    "                --spatialTablePath $projectDir/quantification/exampleSpatialTable.csv \\\n",
    "                --imagePath $projectDir/image/exampleImage.tif \\\n",
    "                --markerChannelMapPath $projectDir/markers.csv \\\n",
    "                --markers ECAD CD3D \\\n",
    "                --maxThumbnails 100 \\\n",
    "                --projectDir $projectDir\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-2: Generate Masks for Training Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "export projectDir=\"/Users/aj/Documents/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                nirmallab/cspot:latest \\\n",
    "                python /app/generateTrainTestSplit.py \\\n",
    "                --thumbnailFolder $projectDir/CSPOT/Thumbnails/CD3D $projectDir/CSPOT/Thumbnails/ECAD\\\n",
    "                --projectDir $projectDir\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-3: Train the CSPOT Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "export projectDir=\"/Users/aj/Documents/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                nirmallab/cspot:latest \\\n",
    "                python /app/csTrain.py \\\n",
    "                --trainingDataPath $projectDir/CSPOT/TrainingData \\\n",
    "                --projectDir $projectDir \\\n",
    "                --epochs=1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-4: Run the CSPOT Algorithm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the `markers.csv` requests to predict on all markers and so replace the current `cspotModel` folder with these models that are [available for download](https://github.com/nirmallab/cspot/tree/main/docs/Tutorials/manuscriptModels/).   \n",
    "  \n",
    "To keep things simple, we're running the entire pipeline with a single command instead of going through the step-by-step process. Nevertheless, you can apply the same principles to each function separately."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "export projectDir=\"/Users/aj/Documents/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                nirmallab/cspot:latest \\\n",
    "                python /app/csPipeline.py \\\n",
    "                --imagePath $projectDir/image/exampleImage.tif \\\n",
    "                --csModelPath $projectDir/CSPOT/cspotModel/ \\\n",
    "                --markerChannelMapPath $projectDir/markers.csv \\\n",
    "                --segmentationMaskPath $projectDir/segmentation/exampleSegmentationMask.tif \\\n",
    "                --spatialTablePath $projectDir/quantification/exampleSpatialTable.csv \\\n",
    "                --projectDir $projectDir \\\n",
    "                --verbose False\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-5: Merge multiple CSPOT objects (optional)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "export projectDir=\"/Users/aj/Documents/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                nirmallab/cspot:latest \\\n",
    "                python /app/mergecsObject.py \\\n",
    "                --csObjects $projectDir/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad $projectDir/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad \\\n",
    "                --projectDir $projectDir\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step-6: CSPOT Phenotyping"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "export projectDir=\"/Users/aj/Dcouments/cspotExampleData\"\n",
    "docker run -it --mount type=bind,source=$projectDir,target=/$projectDir \\\n",
    "                            nirmallab/cspot:latest \\\n",
    "                            python /app/csPhenotype.py \\\n",
    "                            --csObject $projectDir/CSPOT/csObject/exampleImage_cspotPredict.ome.h5ad \\\n",
    "                            --phenotype $projectDir/phenotype_workflow.csv \\\n",
    "                            --projectDir $projectDir\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tutorial ends here. Refer to other tutorials for detailed explanation of each step!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "b155a34da96f173027cc83af6ba86a1d662c2b9e09ee27c56baf0fff8044d14a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
